package net.zoneland.knowledge.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.util.Date;
import java.util.UUID;

/**
 * @author Pbody
 * @create 2019-04-01 11:03
 * @description： token生成、校验工具类
 */
public class JwtUtil {

    // 过期时间8小时
    private static final long EXPIRE_TIME = 8L * 60 * 60 * 1000;

    //签名密匙
    private static final String SECRETKEY="zoneland-jwt-SECRETKEY-2020-4-9 17:36:11";

    /**
     * 生成签名
     *
     * @param username 用户名
     * @param tokenId  redis关联ID
     * @return 加密的token
     */
    public static String sign(String username,String tokenId) {
        try {
            // 指定过期时间
            Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
            Algorithm algorithm = Algorithm.HMAC256(SECRETKEY);
            // 附带username信息
            return JWT.create()
                    .withClaim("username", username)
                    .withClaim("id", tokenId)
                    .withExpiresAt(date)
                    .sign(algorithm);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 校验token是否正确
     *
     * @param token    密钥
     * @param tokenId  token在redis中的唯一ID
     * @return
     */
    public static boolean verify(String token, String tokenId) {
        try {
            //根据密码生成JWT效验器
            Algorithm algorithm = Algorithm.HMAC256(SECRETKEY);
            JWTVerifier verifier = JWT.require(algorithm)
//                    .withClaim("username", username)
                    .withClaim("id", tokenId)
                    .build();
            //效验TOKEN
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 获得token中的登录名
     * @param token
     * @return
     */
    public static String getUsername(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("username").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    
    /**
     * 获得token中的tokenId，与redis中的权限关联
     * @param token
     * @return
     */
    public static String getTokenId(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("id").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    public static void main(String[] args) {
        String tokenId= UUID.randomUUID().toString();
        String tokenInfo= JwtUtil.sign("zhengzq",tokenId);
        System.out.println(tokenId);
        System.out.println(tokenInfo);
        String tokenId1 = getTokenId(tokenInfo);
        System.out.println(tokenId1);
        System.out.println(getTokenId("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjcyY2U0YTE1LWE0ZmMtNGNjOC1hNDEyLWE5ZjY0NTMxMjlkZCIsImV4cCI6MTYyMjYzNjI1OCwidXNlcm5hbWUiOiJjamZ6In0.s_O3SNvS-DJYbPm37quZw6_LRSwIUHJuGlIHa2q68ME"));
//        String id= UUID.randomUUID().toString();
//        System.out.println(id);
//        System.out.println(JwtUtil.sign("cjfz",id));
        System.out.println(JwtUtil.verify("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6IjcyY2U0YTE1LWE0ZmMtNGNjOC1hNDEyLWE5ZjY0NTMxMjlkZCIsImV4cCI6MTYyMjYzNjI1OCwidXNlcm5hbWUiOiJjamZ6In0.s_O3SNvS-DJYbPm37quZw6_LRSwIUHJuGlIHa2q68ME","72ce4a15-a4fc-4cc8-a412-a9f6453129dd"));
    }
}
